<template>
  <view class="send">
    <view class="changeView">
      <view class="changeBtn" @click="toScan">
        扫码激活
      </view>
    </view>
    <view class="cardItem">
      <view>
        <view class="title">请输入卡号</view>
        <view class="uni-input-wrapper">
          <input class="uni-input" v-model.trim="couponIds" placeholder="请输入卡号" />
          <uni-icons type="scan" size="26" class="scanIcon" @click="scan"></uni-icons>
        </view>
      </view>
      <view class="cardButton">
        <view class="btn" @click="handleSubmit">提交</view>
      </view>
    </view>
  </view>
</template>

<script>
import { batchActive } from '@/api/card.js';
export default {
  data() {
    return {
      couponIds: ''
    };
  },
  methods: {
    toScan() {
      uni.navigateTo({
        url: '/pages/scan/scanActive/scanActive'
      });
    },
    scan() {
      // 调用扫码功能
      uni.scanCode({
        success: (res) => {
          console.log('扫码成功:', res);
          this.couponIds = res.result; // 获取扫码结果
        },
        fail: (err) => {
          console.log('扫码失败:', err);
          uni.showToast({
            title: '扫码失败',
            icon: 'none'
          });
        }
      });
    },
    handleSubmit() {
      if (this.couponIds) {
        batchActive({ merchantId: this.userInfo.id, couponIds: this.couponIds }).then((res) => {
          if (res.result['激活成功'] && res.result['激活成功'].length == 1) {
            this.couponIds = '';
            uni.showToast({
              title: '激活成功',
              icon: 'success'
            });
          } else {
            uni.showToast({
              // title: '卡号'+res.result['失败激活卡券'].join(',')+'激活失败',
              title: '激活失败:' + Object.keys(res.result)[0],
              icon: 'none'
            });
          }
        });
      } else {
        uni.showToast({
          title: '请输入卡号',
          icon: 'none'
        });
      }
    }
  },
  computed: {
    userInfo: {
      get() {
        return this.$store.getters.userInfo;
      },
      set() {
      }
    }
  },
  watch: {

  },
  // 页面周期函数--监听页面加载
  onLoad(options) {
    if (options.result) {
      this.couponIds = options.result;
    }
  },
  // 页面周期函数--监听页面初次渲染完成
  onReady() {},
  // 页面周期函数--监听页面显示
  onShow() {},
  // 页面周期函数--监听页面隐藏
  onHide() {},
  // 页面周期函数--监听页面卸载
  onUnload() {},
  // 页面处理函数--监听用户下拉动作
  onPullDownRefresh() {},
  // 页面上拉触底事件的处理函数
  onReachBottom() {},
  // 用户点击右上角分享
  onShareAppMessage() {},
  // 页面处理函数--监听页面滚动(not-nvue)
  onPageScroll(e) {}
};
</script>

<style lang="scss" scoped>
@import "./index.scss";
</style>
